const sha1 = require("sha1")
const config = require("../config")

module.exports = () => {
    return (req,res,next) => {
        console.log(req.query);
        /*
      {
        signature: '7e95c539aa7bff5bb1df5e6d99532154a52e7daa',
        echostr: '7627813802972540923',
        timestamp: '1653959538',
        nonce: '38116457'
      }
      */
      const {signature,echostr,timestamp,nonce} = req.query
      const {token} = config
      // 1. 将`token`、`timestamp`、`nonce`三个参数进行字典序排序 
      const str = [token,timestamp,nonce].sort().join("")
      // 2. 将三个参数字符串拼接成一个字符串进行`sha1`加密 
      const sha1str = sha1(str)
      // 3. 开发者获得加密后的字符串可与 `signature` 对比，标识该请求来源于微信
      if(sha1str == signature){
        console.log(sha1str);
        console.log(signature);
        // 设置头，返回的类型为纯文本
        res.set("Content-Type","text/plain");
        res.send(echostr);
      }else{
        res.send(false)
      }
    }
}